// 档案补录详情页面表单配置文件

// 表单分组配置接口
export interface FormSection {
  key: string;
  title: string;
  icon: string;
  fields: FormField[];
}

// 表单字段配置接口
export interface FormField {
  key: string;
  label: string;
  type: 'text' | 'date' | 'image' | 'select' | 'textarea';
  required?: boolean;
  formatter?: (value: any) => string;
  visible?: (data: any) => boolean;
  fileConfig?: {
    isIdCard?: boolean;
    idCardType?: 'front' | 'back';
  };
}

// 详情列表字段配置接口
export interface DetailField {
  key: string;
  label: string;
  type: 'text' | 'date' | 'dateRange' | 'file';
  formatter?: (value: any, item?: any) => string;
  visible?: (item: any) => boolean;
}

// 计算年限的辅助函数
const calculateYears = (startDate: Date | string | null): number => {
  if (!startDate) return 0;

  try {
    const start = new Date(startDate);
    const now = new Date();

    if (isNaN(start.getTime())) return 0;

    // 计算年份差
    let years = now.getFullYear() - start.getFullYear();

    // 如果当前月日还没到开始日期的月日，年份减1
    const monthDiff = now.getMonth() - start.getMonth();
    if (monthDiff < 0 || (monthDiff === 0 && now.getDate() < start.getDate())) {
      years--;
    }

    // 确保不为负数
    return Math.max(0, years);
  } catch {
    return 0;
  }
};

// 通用格式化函数
export const formatters = {
  // 日期格式化
  date: (value: any) => {
    if (!value) return "-";
    try {
      const date = new Date(value);
      if (isNaN(date.getTime())) return "-";
      return date.toLocaleDateString('zh-CN', {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit'
      });
    } catch {
      return "-";
    }
  },

  // 日期范围格式化
  dateRange: (startDate: any, endDate: any) => {
    const start = formatters.date(startDate);
    const end = endDate ? formatters.date(endDate) : "至今";
    if (start === "-") return "-";
    return `${start} - ${end}`;
  },

  // 默认文本格式化
  text: (value: any, defaultValue: string = "-") => {
    return value || defaultValue;
  },

  // 性别格式化
  gender: (value: any) => {
    return value || "-";
  },

  // 布尔值格式化
  boolean: (value: any) => {
    if (value === true || value === "true") return "是";
    if (value === false || value === "false") return "否";
    return "-";
  }
};

// 获取详情页面的表单分组配置
export function getFormSectionsByPageType(pageType: string): FormSection[] {
  if (pageType === "details") {
    return [
      // 基本信息分组
      {
        key: "basic_info",
        title: "基本信息",
        icon: "lianxiren",
        fields: [
          {
            key: "Name",
            label: "姓名",
            type: "text",
            required: true,
            formatter: (value) => formatters.text(value)
          },
          {
            key: "StaffNo",
            label: "员工工号",
            type: "text",
            required: true,
            formatter: (value) => formatters.text(value)
          },
          {
            key: "IdCardNo",
            label: "身份证号",
            type: "text",
            required: true,
            formatter: (value) => formatters.text(value)
          },
          {
            key: "Gender",
            label: "性别",
            type: "text",
            required: true,
            formatter: (value) => formatters.gender(value)
          },
          {
            key: "BirthDate",
            label: "出生日期",
            type: "date",
            required: true,
            formatter: (value) => formatters.date(value)
          },
          {
            key: "Age",
            label: "年龄",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "Ethnicity",
            label: "民族",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "HighestEducation",
            label: "最高学历",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "MajorCategory",
            label: "专业大类",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "MajorName",
            label: "专业名称",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "MaritalStatus",
            label: "婚姻状况",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "FertilityStatus",
            label: "生育状况",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "BloodType",
            label: "血型",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "IdCardExpiryDate",
            label: "身份证有效期",
            type: "date",
            formatter: (value) => formatters.date(value)
          }
        ]
      },
      // 照片信息分组
      {
        key: "photo_info",
        title: "照片信息",
        icon: "image",
        fields: [
          {
            key: "PhotoId",
            label: "个人照片",
            type: "image",
            required: true,
            fileConfig: {
              isIdCard: false
            }
          },
          {
            key: "IdCardFrontPhotoId",
            label: "身份证正面",
            type: "image",
            required: true,
            fileConfig: {
              isIdCard: true,
              idCardType: "front"
            }
          },
          {
            key: "IdCardBackPhotoId",
            label: "身份证反面",
            type: "image",
            required: true,
            fileConfig: {
              isIdCard: true,
              idCardType: "back"
            }
          },
          {
            key: "VeteranCertificateId",
            label: "退伍证图片",
            type: "image",
            visible: (data) => data?.IsVeteran === true || data?.IsVeteran === "true",
            fileConfig: {
              isIdCard: false
            }
          }
        ]
      },
      // 联系方式分组
      {
        key: "contact_info",
        title: "联系方式",
        icon: "dianhua",
        fields: [
          {
            key: "Phone",
            label: "联系电话",
            type: "text",
            required: true,
            formatter: (value) => formatters.text(value)
          },
          {
            key: "Email",
            label: "邮箱",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "HomeAddress",
            label: "家庭住址",
            type: "text",
            formatter: (value) => formatters.text(value)
          }
        ]
      },
      // 户籍信息分组
      {
        key: "household_info",
        title: "户籍信息",
        icon: "dizhi",
        fields: [
          {
            key: "HouseholdRegistration",
            label: "户口所在地",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "NativePlace",
            label: "籍贯",
            type: "text",
            formatter: (value) => formatters.text(value)
          }
        ]
      },
      // 政治信息分组
      {
        key: "political_info",
        title: "政治信息",
        icon: "xingxing",
        fields: [
          {
            key: "PoliticalStatus",
            label: "政治面貌",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "PartyJoinDate",
            label: "入党(团)日期",
            type: "date",
            formatter: (value) => formatters.date(value)
          },
          {
            key: "PartyBranch",
            label: "入党所在支部",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "ArchiveLocation",
            label: "档案所在地",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "ArchiveNo",
            label: "存档号",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "ArchiveTransferOutDate",
            label: "档案调出日期",
            type: "date",
            formatter: (value) => formatters.date(value)
          },
          {
            key: "ArchiveTransferInDate",
            label: "档案调入日期",
            type: "date",
            formatter: (value) => formatters.date(value)
          },
          {
            key: "ArchiveAgeDate",
            label: "档案年龄日期",
            type: "date",
            formatter: (value) => formatters.date(value)
          },
          {
            key: "IsVeteran",
            label: "是否退伍军人",
            type: "text",
            formatter: (value) => {
              if (value === true || value === "true") return "是";
              if (value === false || value === "false") return "否";
              return "-";
            }
          }
        ]
      },
      // 银行信息分组
      {
        key: "bank_info",
        title: "银行信息",
        icon: "zhifeiji",
        fields: [
          {
            key: "SalaryBank",
            label: "工资卡银行",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "SalaryAccount",
            label: "工资卡号",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "BonusBank",
            label: "奖金卡银行",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "BonusAccount",
            label: "奖金卡号",
            type: "text",
            formatter: (value) => formatters.text(value)
          }
        ]
      },
      // 工作信息分组
      {
        key: "work_info",
        title: "工作信息",
        icon: "gongwenbao",
        fields: [
          {
            key: "Org.Name",
            label: "人事组织",
            type: "text",
            required: true,
            formatter: (value) => formatters.text(value)
          },
          {
            key: "Group.GroupName",
            label: "所属部门",
            type: "text",
            required: true,
            formatter: (value) => formatters.text(value)
          },
          {
            key: "JobPosition.Name",
            label: "工作岗位",
            type: "text",
            required: true,
            formatter: (value) => formatters.text(value)
          },
          {
            key: "JoinWorkDate",
            label: "参加工作时间",
            type: "date",
            formatter: (value) => formatters.date(value)
          },
          {
            key: "WorkYears",
            label: "工龄",
            type: "text",
            formatter: (value) => {
              const years = calculateYears(value);
              return `${years}年`;
            }
          },
          {
            key: "HireDate",
            label: "入职日期",
            type: "date",
            formatter: (value) => formatters.date(value)
          },
          {
            key: "CompanyYears",
            label: "司龄",
            type: "text",
            formatter: (value) => {
              const years = calculateYears(value);
              return `${years}年`;
            }
          },
          {
            key: "ConfirmationDate",
            label: "转正日期",
            type: "date",
            formatter: (value) => formatters.date(value)
          },
          {
            key: "EmploymentType",
            label: "用工形式",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "ContractType",
            label: "劳动合同类型",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "ContractNo",
            label: "合同编号",
            type: "text",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "ContractStartDate",
            label: "合同起始日",
            type: "date",
            formatter: (value) => formatters.date(value)
          },
          {
            key: "ContractEndDate",
            label: "合同终止日",
            type: "date",
            formatter: (value) => formatters.date(value)
          }
        ]
      },
      // 其他信息分组
      {
        key: "other_info",
        title: "其他信息",
        icon: "gongwenbao",
        fields: [
          {
            key: "Hobbies",
            label: "兴趣爱好",
            type: "textarea",
            formatter: (value) => formatters.text(value)
          },
          {
            key: "Remark",
            label: "备注",
            type: "textarea",
            formatter: (value) => formatters.text(value)
          }
        ]
      }
    ];
  }
  return [];
}


